/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.misc.sap.jco.binding;

import com.sap.conn.jco.JCoDestination;

import java.util.HashMap;
import java.util.Map;

/**
 * Jco 会话
 *
 * @author vacoor
 */
public class Session {
    private Map<Class<?>, MapperProxyFactory<?>> cache = new HashMap<Class<?>, MapperProxyFactory<?>>();
    private final JCoDestination destination;

    public Session(JCoDestination destination) {
        this.destination = destination;
    }

    public <T> T getMapper(Class<T> mapperInterface) {
        MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) cache.get(mapperInterface);
        if (mapperProxyFactory == null) {
            mapperProxyFactory = new MapperProxyFactory<T>(mapperInterface);
            cache.put(mapperInterface, mapperProxyFactory);
        }
        return mapperProxyFactory.newProxy(this);
    }

    public JCoDestination getDestination() {
        return destination;
    }
}
